/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
  // 递归后序遍历
    int getHeight(TreeNode* node){
        if(node == nullptr) return 0;
        int left_high = getHeight(node->left);
        if(left_high == -1) return -1;
        int right_high = getHeight(node->right);
        if(right_high == -1) return -1;
        // abs()绝对值
        return abs(left_high - right_high) > 1 ? -1 : 1+max(left_high,right_high); 
    }
    bool isBalanced(TreeNode* root) {
        if(getHeight(root) == -1) return false;
        return true;
    }
};